package cn.fanyamin.model;

/**
 * Base class for Model objects. Inherited class should implement toString(), 
 * equals() and hashCode();
 *
 */
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

/**
 * The Class BaseObject.
 *
 * @author Walter Fan
 */
public abstract class BaseObject implements Serializable, Cloneable {

    /** serialVersionUID. */
    private static final long serialVersionUID = 1L;

    /**
     *  Indicates whether some other object is "equal to" this one.
     * 
     * @param object the reference object with which to compare
     * @return true if this object is the same as the obj argument; false otherwise.
     */
    public boolean equals(Object object) {
        return EqualsBuilder.reflectionEquals(this, object);
    }

    /**
     *  hashCode method
     *
     * @return a hash code value for this object
     */
    public int hashCode() {
        return HashCodeBuilder.reflectionHashCode(this);
    }

    /**
     *  toString method
     * 
     * @return a string representation of the object.
     */
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }

    /**
     * Gets the field.
     *
     * @param strField the str field
     * @return the field
     * @throws IllegalAccessException the illegal access exception
     * @throws InvocationTargetException the invocation target exception
     * @throws NoSuchMethodException the no such method exception
     */
    public Object getField(String strField) throws IllegalAccessException,
            InvocationTargetException, NoSuchMethodException {
        return PropertyUtils.getProperty(this, strField);
    }

    /**
     * Invoke method.
     *
     * @param strMethod the str method
     * @param args the args
     * @return the object
     * @throws IllegalArgumentException the illegal argument exception
     * @throws IllegalAccessException the illegal access exception
     * @throws InvocationTargetException the invocation target exception
     * @throws SecurityException the security exception
     * @throws NoSuchMethodException the no such method exception
     */
    public Object invokeMethod(String strMethod, Object... args)
            throws IllegalArgumentException, IllegalAccessException,
            InvocationTargetException, SecurityException, NoSuchMethodException {
        Method method = this.getClass().getMethod(strMethod, (Class<?>) null);
        return method.invoke(this, args);
    }


    
    
}
